<template>
  <div class="dataCheck w h">
    <div class="w h f-c a-i-c">
      <el-card class="box-b p10 " style="width:600px">
        <lm-form :ref="editFormRefKey" inline label-width="120px" class="lm-w lm-h"
          :layout="formLayout" v-model="editForm">
          <template #mixChange="{ form, item, layout }">
            <div class="w f-s a-i-c">
              <el-input v-model="editForm[item.prop][0]" class="f-g1"></el-input>
              <el-icon-render class="el-icon-close w-100 p4" color="#aaaaaa"
                size="22"></el-icon-render>
              <el-input v-model="editForm[item.prop][1]" class="f-g1"></el-input>
              <span class="p-l6">Pixel</span>
            </div>
          </template>
        </lm-form>
        <div class="result" v-show="tableData.length!=0">
          <span class="title">操作结果</span>
          <el-table :data="tableData" stripe style="width: 100%">
    <el-table-column prop="image_id" label="图片ID"  />
     <el-table-column prop="label_id" label="标签ID" />
    <el-table-column prop="anno_id" label="框体ID"  />
    <el-table-column prop="check_type" label="判断条件" />
       <el-table-column  label="操作结果" >已删除</el-table-column>
  </el-table>
        </div>
        <div class="lm-w lm-h lm-f-s">
          <div class="lm-w-120 lm-h"></div>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button type="danger" @click="reset" plain>重置</el-button>
        </div>
      </el-card>
    </div>
  </div>
</template>
<script lang="ts">
import {dataCheck} from "@/apis/dataCheck"
import {Collection} from "@/apis/collection"
import {TagGroup,type TagGroupInfo,Tag,type TagInfo} from "@/apis/tag"
import { RequestCode } from '@/server/base'
import { type ValidateFieldsError } from 'async-validator'
import { FormLayout } from './res'
import ElIconRender from '@/components/render/el-icon-render'

export default {
  name: 'DataCheck',
  components: { ElIconRender },
  data() {
    return {
      editFormRefKey: 'dataCheckRef' as string,
       tagGroupList:[] as TagGroupInfo[],
      editForm: { mixChange: [] } as any,
      tagList: [] as TagInfo[],
      formLayout: FormLayout as any[],
      labelIdList:[] as number [],
      imgSlect:[] as any,
      tableData:[] as any,
      

    }
  },
  methods: {
       imageCollection(){
          Collection.imageCollection('','').then((res:RequestCode)=>{
            let data:any=res.data
            data.forEach((item:any)=>{
              this.formLayout.forEach((items:any)=>{
                if(items.prop=='photoListId'){
                  items.options.push({value:item.image[0].photo.id,label:item.image[0].photo.name})
                }
              })
            })
          })
        
      },
    getTagGroup() {
       TagGroup.list({
        current: 1,
        size: 998,
        keyword: ''}).then((res:RequestCode)=>{  
            this.tagGroupList=res.data.records
            this.tagGroupList.forEach((item:any)=>{
              this.labelIdList.push(item.id)
            })
            this.getTagList(this.labelIdList)
        })
    },
             //获取所有标签组的标签
         getTagList(val :number []){
           this.formLayout[1].options=[]
                 let str : string=''
               val?.forEach(item=>{
                str+=item+','
             })
              str=str.substring(0,str.lastIndexOf(','))
              if(str===''){
                 this.tagList=[] 
              }else{
                 Tag.listByLabelGroup({id:str}).then((res:RequestCode)=>{
                 this.tagList = res.data
                 this.tagList.forEach((item:any)=>{
                   this.formLayout[1].options.push({value:item.id,label:item.name})
                 })
                 
             })
              }  
         },
    submitInfo() {
    
      this.editForm.mixChange.forEach((item:any)=>{
        item=parseInt(item)
      })
       
      let params={
        imgList:this.editForm.photoListId,
        checkClass:this.editForm.tagList,
        super5ma11Box: this.editForm.mixChange,
        overstepBox:this.editForm.borderOverstep,
        overlapBox: this.editForm.borderOverlap
      }
      dataCheck.autoCheckLabel(params).then((res:RequestCode)=>{
        (this as any).$message.success('检查成功')
        this.tableData=res.data.delete_data

      })
    },
    //验证数据
    submit() {
      const editFormRef = this.$refs[this.editFormRefKey] as any
      editFormRef.validate((isValid: boolean, invalidFields?: ValidateFieldsError) => {
        if (!isValid) {
          //滚动到未通过校验的字段
          if (invalidFields && Object.keys(invalidFields).length > 0) {
            editFormRef.scrollToField(Object.keys(invalidFields)[0])
          }
          return
        }
        this.submitInfo()
      })
    },
    //重置
    reset() {
       this.editForm = { mixChange: [] }
    }
  },
  created() {
    this.imageCollection()
    this.getTagGroup()
  }
}
</script>
<style lang="scss" scoped>
.result{
  display: flex;
  align-items: start;
  margin-bottom: 15px;
   max-height: 300px;
   overflow: auto;
  .title{
    font-size: 14px;
    width: 100px;
    margin-top: 8px;
  }
}
</style>